package vip.denglei.company.util.kuidi100;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.kuaidi100.sdk.pojo.HttpResult;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author: api.kuaidi100.com
 * @Date: 2020-07-14 16:59
 */
public class HttpUtils {

    private final static String CHARSET_DEFAULT = "UTF-8";

    /**
     * post请求  编码格式默认UTF-8
     *
     * @param url 请求url
     * @return
     */
    public static HttpResult doPost(String url, Object obj) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse resp = null;

        HttpResult result = new HttpResult();
        try {
            Map<String, String> params = ObjectToMapUtils.objectToMap(obj);
            HttpPost httpPost = new HttpPost(url);
            if (params != null && params.size() > 0) {
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                httpPost.setEntity(new UrlEncodedFormEntity(list, CHARSET_DEFAULT));
            }

            resp = httpClient.execute(httpPost);
            String body = EntityUtils.toString(resp.getEntity(), CHARSET_DEFAULT);
            int statusCode = resp.getStatusLine().getStatusCode();
            result.setStatus(statusCode);
            result.setBody(body);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != resp) {
                try {
                    resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * post请求  编码格式默认UTF-8
     *
     * @param url     请求url
     * @return
     */
    public static HttpResult doPostFile(String url,  File file) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse resp = null;

        HttpResult result = new HttpResult();
        try {

            HttpPost httpPost = new HttpPost(url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            // 把文件加到HTTP的post请求中
            builder.addBinaryBody(
                    "file",
                    new FileInputStream(file),
                    ContentType.MULTIPART_FORM_DATA,
                    file.getName()
            );
            //HttpEntity
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            resp = httpClient.execute(httpPost);
            result.setStatus(resp.getStatusLine().getStatusCode());
            result.setBody(EntityUtils.toString(resp.getEntity(), CHARSET_DEFAULT));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != resp) {
                try {
                    resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    public static String  post(String urlStr, Map<String, Object> params)  {
        StringBuilder  response  =  new  StringBuilder("");
        BufferedReader reader  =  null;
        try  {
            StringBuilder  builder  =  new  StringBuilder();
            for  (Map.Entry  param  :  params.entrySet())  {
                if  (builder.length()  >  0)  {
                    builder.append('&');
                }
                builder.append(URLEncoder.encode(StrUtil.toUnderlineCase(String.valueOf(param.getKey())),  "UTF-8"));
                builder.append('=');
                builder.append(URLEncoder.encode(String.valueOf(param.getValue()),  "UTF-8"));
            }
            String response1= HttpUtil.post(ApiInfoConstant.CLOUD_NORMAL_URL, String.valueOf(builder));
            System.out.println(response1);

            byte[]  bytes  =  builder.toString().getBytes("UTF-8");
            URL url  =  new  URL(urlStr);
            HttpURLConnection conn  =  (HttpURLConnection)  url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept",  "*/*");
            conn.setRequestProperty("connection",  "Keep-Alive");
            conn.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",  String.valueOf(bytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(bytes);
            reader  =  new  BufferedReader(new InputStreamReader(conn.getInputStream(),  "UTF-8"));
            String  line  =  "";
            while ((line  =  reader.readLine())  !=  null)  {
                response.append(line);
            }
        }  catch  (Exception  e)  {
            e.printStackTrace();
        }  finally  {
            try  {
                if  (null  !=  reader)  {
                    reader.close();
                }
            }  catch  (IOException  e)  {
                e.printStackTrace();
            }
        }
        return  response.toString();
    }


}
